package com.lingtu.shopwxserver.controller;

import com.lingtu.shopwxserver.utils.ImageMessageUtil;
import com.lingtu.shopwxserver.utils.MessageUtil;
import com.lingtu.shopwxserver.utils.TextMessageUtil;
import com.lingtu.shopwxserver.utils.SignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @ClassName WechatController
 * @Description 连接微信服务器
 * @Author MaShuai
 * @Date 2018/11/21 10:42
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "login")
public class WechatController {

    private static Logger log = LoggerFactory.getLogger(WechatController.class);

    /**
     * @Description get请求
     * @Param [request, response]
     * @return void
     */
    @RequestMapping(method = { RequestMethod.GET })
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        log.debug("wechat get...");

        // 微信加密签名
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");

        // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        PrintWriter out = null;
        try {
            out = response.getWriter();
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                log.debug("wechat get success....");
                out.print(echostr);
                System.out.println("微信服务验证成功！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    @RequestMapping(method=RequestMethod.POST)
    public void dopost(HttpServletRequest request,HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        // 将微信请求xml转为map格式，获取所需的参数
        Map<String,String> map = MessageUtil.xmlToMap(request);
        // 开发者微信公众号
        String ToUserName = map.get("ToUserName");
        // 发送方账号
        String FromUserName = map.get("FromUserName");
        // 消息类型
        String MsgType = map.get("MsgType");
        // 消息内容
        String Content = map.get("Content");

        String message = null;
        // 处理文本类型,回复相应的封装的内容
        if("text".equals(MsgType)){
            if("美女".equals(Content)){
                ImageMessageUtil imageMessageUtil = new ImageMessageUtil();
                message = imageMessageUtil.initMessage(FromUserName, ToUserName);
            } else {
                TextMessageUtil textMessageUtil = new TextMessageUtil();
                message = textMessageUtil.initMessage(FromUserName, ToUserName);
            }
        }

        try {
            out = response.getWriter();
            out.write(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.close();
    }


}
